1637C - Andrew and Stones - CodeForces Solution


greedy implementation *1200

Please click on ads to support us..

Python Code:

def main():
    
                t = int(input())
    allans = []
    for _ in range(t):
        n = int(input())
        a = readIntArr()
        b = []
        for i in range(1, n - 1):
            b.append(a[i])
        b.sort(reverse = True)
        ans = 0
        for x in b:
            ans += (x + 1) // 2
        if len(b) == 1:
            if b[0] % 2 == 1:
                ans = -1
        elif b[0] % 2 == 1:
            if b[1] == 0:
                if b[0] % 2 == 1 and b[0] < 5:
                    ans = -1
                else:
                    ans += 1             elif b[1] == 1 and b[0] == 1:
                ans = -1
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return
 
import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a, b, c, i, j, k, m, n, o, l, r, f = 0, cnt = 0, even, odd, sum = 0,ans, total = 0, mn = INT_MAX, mx = INT_MIN, sz, len, temp, pos, tc, x, y, z, arr[100000], brr[100000];
    vector<long long int> vrr;
    string s;

    cin>>tc;
    while(tc--)
    {
        cin>>n;
        even = 0;
        odd = 0;
        for( i = 1; i<=n; i++ )
        {
            cin>>arr[i];
        }

        bool has_even=false,all_one=true,possible=true;

        ans = 0 ;
        for( i = 2; i<n; i++ )
        {
            if( arr[i] != 1 )  all_one=false;
            if(arr[i]%2==0)
            {
                has_even=true;
            }


            ans+=((arr[i]+1)/2);

        }

         if(all_one==true || (n==3 and !has_even)){
            possible=false;
        }

        if( possible ) cout<< ans << endl;
        else cout<< -1 << endl;
    }



}


Comments

Submit
0 Comments
More Questions

535B - Tavas and SaDDas
722C - Destroying Array
366A - Dima and Guards
716B - Complete the Word
1461C - Random Events
1627A - Not Shading
141B - Hopscotch
47B - Coins
1466C - Canine poetry
74A - Room Leader
1333D - Challenges in school №41
1475B - New Year's Number
461A - Appleman and Toastman
320B - Ping-Pong (Easy Version)
948A - Protect Sheep
387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag